package com.qianxun.spider.monitor;

import com.qianxun.spider.Spider;
import com.qianxun.spider.config.SpiderBeanConfig;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.nodes.Document;

import java.util.Map;

/**
 * @author: huwei
 * @date: 2020/6/29 16:53
 * @version: 1.0.0
 */
@Slf4j
public class ConsoleSpiderMonitor implements SpiderMonitor {
    @Override
    public void requestBefore(Connection connection, SpiderBeanConfig spiderBeanConfig, Spider spider , Spider.ThreadLocalData localData) {
        System.err.println("请求"+localData.getCurrentRequestUrl()+"数据前置日志");
    }

    @Override
    public void requestAfter(Connection connection, SpiderBeanConfig spiderBeanConfig, Spider spider , Spider.ThreadLocalData localData) {
        System.err.println("请求"+localData.getCurrentRequestUrl()+"数据后置日志");
    }

    @Override
    public void requestError(Connection connection, SpiderBeanConfig spiderBeanConfig, Spider spider , Spider.ThreadLocalData localData, Throwable throwable) {
        System.err.println("请求"+localData.getCurrentRequestUrl()+"数据错误日志;错误信息：" + throwable.getMessage() );
    }

    @Override
    public void parseBefore(Document document, SpiderBeanConfig spiderBeanConfig, Spider spider , Spider.ThreadLocalData localData) {
        System.err.println("解析"+localData.getCurrentRequestUrl()+"数据前置日志");
    }

    @Override
    public void parseAfter(Document document, SpiderBeanConfig spiderBeanConfig, Map<String, Object> parseData, Spider spider , Spider.ThreadLocalData localData) {
        System.err.println("解析"+localData.getCurrentRequestUrl()+"数据后置日志，解析数据:" + parseData);
    }

    @Override
    public void parseError(Document document, SpiderBeanConfig spiderBeanConfig, Spider spider , Spider.ThreadLocalData localData, Throwable throwable) {
        System.err.println("解析"+localData.getCurrentRequestUrl()+"数据错误日志，错误信息:" + throwable.getMessage());
    }

    @Override
    public void close(SpiderBeanConfig spiderBeanConfig) {
        System.err.println("spider任务关闭了");
    }
}
